package com.ok.runnable;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpGlobalConfig;
import com.ok.event.WsEvent;
import com.ok.event.WsEventManage;
import com.ok.thread.BaseThreadPlus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
@Slf4j
public class TimerTaskStart implements CommandLineRunner {
    @Resource
    private ApplicationContext applicationContext;

    @Override
    public void run(String... args) throws Exception {

        // 设置http超时
        HttpGlobalConfig.setTimeout(60 * 1000);

        //注册所有ws事件
        Map<String, WsEvent> wsEventBeans = applicationContext.getBeansOfType(WsEvent.class);
        if (MapUtil.isNotEmpty(wsEventBeans)) {
            log.info("注册的事件数:{}", wsEventBeans.size());
            for (WsEvent wsEvent : wsEventBeans.values()) {
                log.info("注册的事件:{}", wsEventBeans.values());
                WsEventManage.registerEventListen(wsEvent);
            }
        }
        log.info("初始化ws事件处理器完成");
        //websocket前端心跳
        BaseThreadPlus wsWsHeartbeat = BaseThreadPlus.loadBaseThreadPlus(WsHeartbeatRunnable.class.getSimpleName(),
                WsHeartbeatRunnable.class);
        wsWsHeartbeat.startPlus();
        log.info("websocket心跳定时任务已启动");
    }
}
